﻿namespace C12_Ex03_Shaked_032675316_Itay_032511057
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;

    [Serializable]
    public class Mapper<T1, T2> : ISerializable
    {
        private Dictionary<T1, T2> m_Dictionary;

        #region Static Methods

        public static void Serialize(string i_filename, Mapper<T1, T2> i_MapperToSerialize)
        {
            Stream stream = File.Open(i_filename, FileMode.Create);
            BinaryFormatter bFormatter = new BinaryFormatter();
            bFormatter.Serialize(stream, i_MapperToSerialize);
            stream.Close();
        }

        public static Mapper<T1, T2> DeSerialize(string i_filename)
        {
            Mapper<T1, T2> loadedObj;
            Stream stream = File.Open(i_filename, FileMode.Open);
            BinaryFormatter bFormatter = new BinaryFormatter();
            loadedObj = (Mapper<T1, T2>)bFormatter.Deserialize(stream);
            stream.Close();
            return loadedObj;
        }

        #endregion

        public Mapper()
        {
            m_Dictionary = new Dictionary<T1, T2>();
        }

        public Mapper(SerializationInfo info, StreamingContext ctxt)
        {
            m_Dictionary = (Dictionary<T1, T2>)info.GetValue("Dictionary", typeof(Dictionary<T1, T2>));
        }

        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            info.AddValue("Dictionary", m_Dictionary);
        }

        public Dictionary<T1, T2> InnerDictionary
        {
            get
            {
                return m_Dictionary;
            }
        }
    }
}